﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Windows.Forms;
using System.Xml;

namespace PPad
{
    public partial class Preferences : Form
    {
        public Preferences()
        {
            InitializeComponent();
        }

        private void Preferences_Load(object sender, EventArgs e)
        {
            treeView1.Nodes[0].ExpandAll();
            
            groupBox1.Left = 199;
            groupBox1.Top = 0;
            groupBox2.Left = 199;
            groupBox2.Top = 0;
            groupBox3.Left = 199;
            groupBox3.Top = 0;



        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            groupBox1.Text = treeView1.SelectedNode.Text;
            if (treeView1.SelectedNode.Text == "Preferences")
            {
                groupBox1.Visible = true;
                groupBox2.Visible = false;
                groupBox3.Visible = false;
            }
            if (treeView1.SelectedNode.Text == "Aplication")
            {
                groupBox2.Visible = true;
                groupBox1.Visible = false;
                groupBox3.Visible = false;
            }
            if (treeView1.SelectedNode.Text == "Documents")
            {
                groupBox3.Visible = true;
                groupBox1.Visible = false;
                groupBox2.Visible = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (radioNormal.Checked)
            {
                editConfig("windowState", "Normal");
            }
            if (radioMaximized.Checked)
            {
                editConfig("windowState", "Maximized");
            }
            if (radioMinimized.Checked)
            {
                editConfig("windowState", "Minimized");
            }
            this.Close();
        }

        public void editConfig(string key, string value)
        {
            XmlDocument Config = new XmlDocument();
            string path = Application.StartupPath+"\\..\\..\\app.config";
            Config.Load(path);

            XmlNodeList apps = Config.GetElementsByTagName("appSettings");
            XmlNodeList list = ((XmlElement)apps[0]).GetElementsByTagName("add");
            foreach (XmlElement node in list)
            {
                if (node.GetAttribute("key") == key)
                {
                    node.SetAttribute("value", value);
                    break;
                }
            }
            Config.Save(path);
        }

        private void radioNormal_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
